const UserModel = require("../models/usersModels");
const { successResponse, errorResponse } = require("../utils/response");

class UserController {
  static async getUsers(req, res) {
    // console.log(req.query,'query')
    try {
      const { id } = req.query;

      const users = await UserModel.getUsers(id);
    //   console.log(users,'1233333333333333333333333333333333')
      return res.send(successResponse(users));
    } catch (error) {
      return res
        .status(500)
        .send(errorResponse("Failed to fetch users", error.message));
    }
  }

  static async addUsers(req, res) {
    try {
      const {
        user_id,
        user,
        user_name,
        password,
        age,
        birth,
        phone,
        position,
        address,
        entry_time,
      } = req.body;
      const users = await UserModel.addUsers({
        user_id,
        user,
        user_name,
        password,
        age,
        birth,
        phone,
        position,
        address,
        entry_time,
      });

      return res.send(successResponse(users));
    } catch (error) {
      return res
        .status(500)
        .send(errorResponse("Failed to add users", error.message));
    }
  }

  static async updateUsers(req, res) {
    try {
      const { id } = req.query;
      const data = req.body;
      const users = await UserModel.updateUsers(id, data);
      return res.send(successResponse(users));
    } catch (error) {
      return res
        .status(500)
        .send(errorResponse("Failed to fetch users", error.message));
    }
  }

  static async deleteUsers(req, res) {
    try {
      const { id } = req.query;
      const users = await UserModel.deleteUsers(id);
      return res.send(successResponse(users));
    } catch (error) {
      return res
        .status(500)
        .send(errorResponse("Failed to fetch users", error.message));
    }
  }
}

module.exports = UserController;
